#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <dirent.h>
#include <string.h>

#define BUFFER_SIZE 64


void musicDir(char * str)
{
    DIR * dir = opendir(str);
    if(dir == NULL)
    {
        perror("open str error\n");
        exit(-1);
    }
    while(1)
    {
        struct dirent * content = readdir(dir);
        if(content != NULL)
        {
            if(strcmp(content->d_name,".") == 0 || strcmp(content->d_name,"..") == 0)
                continue;
            if(content->d_type == 4 )
            {   
                char ptr[256] = {0};
                strcpy(ptr,str); 
                strcat(ptr,"/");
                musicDir(strcat(ptr,content->d_name));
            }
            else if(content->d_type == 8 && strstr(content->d_name,".mp3") != NULL)
            {
                printf("name:%s, type:%d\n",content->d_name,content->d_type);  
            }
           // printf("name:%s, type:%d\n",content->d_name,content->d_type);  
        }
        else
            break;
    }

    closedir(dir);

}


int main()
{
    char pathname[BUFFER_SIZE] = {0};
    /* 获取当前进程的工作目录 */
    getcwd(pathname,BUFFER_SIZE);
    
    printf("pathname = %s\n",pathname);

    /* 打开文件夹 */
    DIR * dir = opendir(pathname);
    if(dir == NULL)
    {
        perror("open pathname error\n");
        exit(-1);
    }

    /* 读取文件夹 */

    while(1)
    {
        struct dirent * content = readdir(dir);
        if(content != NULL)
        {
            if(strcmp(content->d_name,".") == 0 || strcmp(content->d_name,"..") == 0)
                continue;
   
            if(content->d_type == 4 )
            {   
                char ptr[256] = {0};
                strcpy(ptr,pathname); 
                strcat(ptr,"/");
                musicDir(strcat(ptr,content->d_name));
                
            }
            else if(content->d_type == 8 && strstr(content->d_name,".mp3") != NULL)
            {
                printf("name:%s, type:%d\n",content->d_name,content->d_type);  
            }
           // printf("name:%s, type:%d\n",content->d_name,content->d_type);   
        }
        else
            break;

    }


    /* 关闭文件夹 */
    closedir(dir);

    return 0;
}